package com.xms.sdk.api.controller;

import com.xms.schema.service.attribute.IAttributeFinderService;
import com.xms.schema.service.entity.IEntityFinderService;
import com.xms.sdk.service.IDataCreater;
import com.xms.web.framework.controller.ApiBaseController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * DataCreaterController
 * </p>
 *
 * @author migo
 * @since 2021/9/14
 */
@RestController
@RequestMapping("{org}/api/data")
public class DataCreaterController extends ApiBaseController {
    private IEntityFinderService _entityFinderService;
    private IAttributeFinderService _attributeFinderService;
    private IDataCreater _dataCreater;

    public DataCreaterController(IEntityFinderService entityFinderService
            , IAttributeFinderService attributeFinderService
            , com.xms.sdk.service.IDataCreater dataCreater)
    {
        _entityFinderService = entityFinderService;
        _attributeFinderService = attributeFinderService;
        _dataCreater = dataCreater;
    }

    /*@ApiOperation("创建记录")
    @PostMapping()
    public JsonResultObject Post(CreateRecordModel model)
    {
        if (model.Data.IsEmpty())
        {
            return JError("data is empty");
        }
        Schema.Domain.Entity entityMeta = null;
        if (model.EntityId.HasValue && !model.EntityId.Value.Equals(Guid.Empty))
        {
            entityMeta = _entityFinder.FindById(model.EntityId.Value);
        }
        else if (model.EntityName.IsNotEmpty())
        {
            entityMeta = _entityFinder.FindByName(model.EntityName);
        }
        if (entityMeta == null)
        {
            return NotFound();
        }
        var childAttributes = _attributeFinder.FindByEntityName(entityMeta.Name);
        if (model.Data.StartsWith("["))
        {
            var details = new List<Entity>();
            var items = JArray.Parse(model.Data.UrlDecode());
            if (items.Count > 0)
            {
                foreach (var c in items)
                {
                    dynamic root = JObject.Parse(c.ToString());
                    Entity detail = new Entity(entityMeta.Name);
                    foreach (JProperty p in root)
                    {
                        var attr = childAttributes.Find(n => n.Name.IsCaseInsensitiveEqual(p.Name));
                        if (attr != null && p.Value != null)
                        {
                            detail.SetAttributeValue(p.Name.ToString().ToLower(), detail.WrapAttributeValue(_entityFinder, attr, p.Value.ToString()));
                        }
                    }
                    details.Add(detail);
                }
            }
            return _dataCreater.CreateMany(details).CreateResult(T);
        }
        else
        {
            dynamic root = JObject.Parse(model.Data.UrlDecode());
            Entity detail = new Entity(entityMeta.Name);
            foreach (JProperty p in root)
            {
                var attr = childAttributes.Find(n => n.Name.IsCaseInsensitiveEqual(p.Name));
                if (attr != null)
                {
                    detail.SetAttributeValue(p.Name.ToString().ToLower(), detail.WrapAttributeValue(_entityFinder, attr, p.Value.ToString()));
                }
            }
            var id = _dataCreater.Create(detail);
            return CreateSuccess(new { id = id });
        }
    }*/
}
